package com.fmatos.amazingwallpapers.image.database;

import java.util.ArrayList;
import java.util.List;

import com.fmatos.amazingwallpapers.image.database.dto.AlbumRow;
import com.fmatos.amazingwallpapers.image.database.dto.ImageRow;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

@Deprecated
public class PhotosDataSource extends DataSource {

	private static final String TAG = PhotosDataSource.class.getSimpleName();

	public PhotosDataSource(Context context) {
		super(context,new PhotosDatabaseOpenHelper(context));

	}
	
	private ImageRow cursorToImage(Cursor cursor) {
		ImageRow row = new ImageRow();
		
		row.setUrl(cursor.getString(0));
		row.setAlbum(cursor.getString(1));
		
		row.setFile(cursor.getString(2));
		row.setCaption(cursor.getString(3));
		row.setDescription(cursor.getString(4));
//		row.setTimestamp(cursor.getString(5));
//		Log.d(TAG,"Cursor got image " + row.getId() + " with url " + row.getUrl() + " and caption " + row.getCaption());
		return row;
	}
	
	public void createImageRow(ImageRow photo) {

		if ( ! isOpen() || photo == null ) {
			return;
		}
		
		if ( isRowExists(photo.getUrl()) ) {
			Log.v(TAG,"Row was found for name " + photo.getUrl() + ", skipping insert");
			return;
		}
		
		ContentValues values = new ContentValues();
		values.put(PhotosDatabaseOpenHelper.COLUMN_ALBUM, photo.getAlbum());
		values.put(PhotosDatabaseOpenHelper.COLUMN_URL,photo.getUrl());
		
		values.put(PhotosDatabaseOpenHelper.COLUMN_CAPTION,photo.getCaption());		
		values.put(PhotosDatabaseOpenHelper.COLUMN_DESCRIPTION, photo.getDescription());
		

		try { 
			long insertId = getDatabase().insertOrThrow(PhotosDatabaseOpenHelper.TABLE_IMAGES, 
					null, values);
			if ( insertId != -1 ) {
				Log.i(TAG,"Tried insert on photo (" + photo.getCaption() + ") album (" +  photo.getAlbum() + ")");
			}		

		} catch ( SQLiteConstraintException e ) {
			Log.d(TAG,"Failed inserting album inserted (" + photo.getCaption() + ") album (" +  photo.getAlbum() + ")");
		}

		
	}

	/**
	 * Checkis if given url is already on the DB
	 * @param url
	 * @return
	 */
	private boolean isRowExists(String url) {
		
		long nRows = DatabaseUtils.queryNumEntries(getDatabase(), PhotosDatabaseOpenHelper.TABLE_IMAGES,
				buildWhereClause(PhotosDatabaseOpenHelper.COLUMN_URL, url) );
		
		return nRows > 0;
	}

	public List<ImageRow> getAllPhotosFromAlbum(String albumName) {
		
		if ( !isOpen() || albumName == null) {
			return null;
		}
		
		List<ImageRow> photosList = new ArrayList<ImageRow>();

		String groupBy = null;
		String whereClause = buildWhereClause(PhotosDatabaseOpenHelper.COLUMN_ALBUM,albumName);
		String orderBy = null;
		
		Cursor cursor = getDatabase().query(true, PhotosDatabaseOpenHelper.TABLE_IMAGES,
				null,whereClause, 
				null,
				groupBy ,null,orderBy, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			ImageRow comment = cursorToImage(cursor);
			photosList.add(comment);
			cursor.moveToNext();
		}
		// make sure to close the cursor
		cursor.close();
		
		Log.i(TAG,"Got " + photosList.size() + " images from DB on album " + albumName );
		return photosList;
		
	}

}
